#require "rjb"

class IndexController < ApplicationController
  before_filter :user_authorize,:except =>[:index,:login,:person_index,:person_login,:happy,:happy_person,:happy_welcome,:agent_login,:agent_index,:web_logout,:agent_logout,:person_logout]
  #before_filter :explorer_info
  layout "index",:except => [:index,:person_index,:person_login,:explorer_info,
    :checkAir,:checkAir2,:checkZjly,:searchAirLine,:searchZjly,:login,:happy,
    :happy_person,:welcome,:happy_welcome,:web_index,:web_login,:web_logout,
    :agent_index,:agent_login,:searchZjlyAirLine,:person_logout,:agent_logout]
  
  #仅在login,main,index中调用
  def explorer_info
    begin
      explorerInfo = request.env["HTTP_USER_AGENT"]
      if explorerInfo
        if explorerInfo.index("MSIE")!=nil
          session[:explorer_info] = "IE"
        else
          session[:explorer_info]  = "FF"
        end
      else
        session[:explorer_info] = "IE"
      end
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def index
    explorer_info
  end
  
  def happy
    explorer_info
    if session[:web_user_id] ==nil
      redirect_to(:controller => "index", :action => "web_index") and return
    end
  end
  
  #员工登录
  def login
    begin
      explorer_info
      user_name = params[:user_name]
      password = params[:password]
      user = User.find(:first,:conditions => ["login_id = ? and password = ? AND is_locked <> 1",user_name,password])
      if user == nil
        info="用户名和密码不匹配"
      else
        session[:user_id] = user.id
        session[:user_name] = user.user_name
        session[:user_type] = user.user_type
        session[:employee_no] = user.employee_no
        session[:eterm_name] = user.eterm_name
        session[:eterm_pswd] = user.eterm_pswd
        session[:latest_chat_id] = user.latest_chat_id
        #获取区域
        department = user.department
        if department && department.area
          session[:area] = department.area
        else
          session[:area] = ""
        end
        #处理权限
        if user.rights.length!=0
          rightHash = Hash.new
          user.rights.each do |right|
            rightHash[right.controller_name+"-"+right.action_name]=1
          end
          session[:user_rights]=rightHash
        end
        info="登录成功"
      end
      result = "{success:true,info:'#{info}'}"
      render :text=>result, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def person_login
    begin
      explorer_info
      user_name = params[:user_name]
      password = params[:password]
      puts user_name
      puts password
      customer = Customer.find(:first,:conditions => ["login_id = ? and password = ? AND is_locked <> 1",user_name,password])
      if customer == nil
        info="用户名和密码不匹配"
      else
        session[:web_user_id] = customer.id
        session[:web_user_name] = customer.name
        #session[:web_user_type] = customer.user_type
        info="登录成功"
      end
      result = "{success:true,info:'#{info}'}"
      render :text=>result, :layout=>false
    rescue Exception => e
      puts e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def agent_index
    logger.error "========"
  end
  
  #代理人登录
  def agent_login
    begin
      logger.error "========== agent_login ============="
      #explorer_info
      user_name = params[:user_name]
      password = params[:password]

      logger.error user_name
      logger.error password

      webUser = WebUser.find(:first,:conditions => ["login_id = ? and password = ?",user_name,password])
      if webUser == nil
        info="用户名和密码不匹配|"
        url=""
      else
        session[:web_user_id] = webUser.id
        session[:web_login_id] = webUser.login_id
        session[:web_user_name] = webUser.user_name
        session[:user_type] = webUser.user_type
        #        if user.rights.length!=0
        #          session[:user_rights]=user.rights
        #        end
        info = "登录成功|"
        if webUser.user_type == "ADMIN"
          url = "/web_admin/admin_index"
        else
          url = "/index/happy"
        end
      end
      result = "{success:true,info:'#{info}',url:'#{url}'}"
      logger.error result
      render :text=>result, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def logout
    reset_session
    redirect_to(:controller => "index", :action => "index") and return
  end
  
  def person_logout
    reset_session
    redirect_to(:controller => "index", :action => "person_index") and return
  end
  
  def agent_logout
    reset_session
    redirect_to(:controller => "index", :action => "agent_index") and return
  end
  # Show All TABS BY ID
  def phoneshow
    begin
      #允许在来电时直接登录
      explorer_info
      user_name = params[:user_name]
      password = params[:password]
      webUser = WebUser.find(:first,:conditions => ["user_name = ? and password = ?",user_name,password])
      if webUser == nil
        jsonstr="用户名和密码不匹配|"
      else
        session[:web_user_id]=webUser.id
        session[:web_user_name]=webUser.user_name
        session[:user_type]=webUser.user_type
      end
      
      id = params[:id]
      phone = params[:phone_number]
      oid = params[:oid]
      @type=params[:type]
      if @type == nil
        @type = "repeat"
      end
      @option = params[:option]
      if @option == nil
        @option = ""
      end

      #logger.error ">>>>>>>> phoneshow >>>>>>>>>>"
      #logger.error id
      #logger.error phone
      #logger.error oid
      #logger.error @option

      @orderid = ""
      @orderid = oid if oid && oid.to_i>0
      @phone_number = ""
      if phone && phone!=""
        @phone_number = phone
        session[:phone_number] = @phone_number
      elsif id
        customer = Customer.find_by_id(id)
        @phone_number = customer.phone
      end
      if session[:phone_number] && @phone_number == ""
        @phone_number = session[:phone_number]
      end
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def checkAir
    begin
      id = params[:id]
      phone = params[:phone_number]
      
      @from = ""
      @to = ""
      @to2 = ""
      @mid = ""
      @from_airport = ""
      @to_airport = ""
      @to2_airport = ""
      @mid_airport = ""
      @trip_type = ""
      @airways_name = ""
      @airways_code = ""
      @position = ""
      @position2 = ""
      if params[:trip_type]
        @trip_type = params[:trip_type]
        from_id = params[:from_id]
        mid_id = params[:mid_id]
        mid_id2 = params[:mid_id2]
        to_id = params[:to_id]
        airways_id = params[:airways_id]
        @position = params[:position]
        @position2 = params[:position2]
        
        from = Airport.find_by_id(from_id)
        mid = Airport.find_by_id(mid_id)
        to = Airport.find_by_id(to_id)
        airways = Airways.find_by_id(airways_id)
        
        @from = from.airport_code
        @from_airport = from.airport_name
        @to = mid.airport_code
        @to_airport = mid.airport_name
        if @trip_type=="lc" || @trip_type=="wflc"
          @to2 = to.airport_code
          @to2_airport = to.airport_name
          if mid_id2.to_i>0
            mid2 = Airport.find_by_id(mid_id2)
            @mid = mid2.airport_code
            @mid_airport = mid2.airport_name
          end
        end
        @airways_name = airways.airways_name
        @airways_code = airways.airways_code
      end
      @orderid = ""
      @order_no = "";
      @orderid = params[:orderid] if params[:orderid]
      @order = Order.find_by_id(@orderid) if @orderid && @orderid.to_i>0
      if @order
        @order_no = @order.order_no
        @customer = @order.customer
      elsif id && id.to_i>0
        @customer = Customer.find_by_id(id)
      elsif phone && phone!=""
        @customer = Customer.find(:first,:conditions=>["phone=?",phone])
      end
      @customer = Customer.find_by_id(1,:include=>[:customer_type]) if @customer==nil
      #logger.error @customer
      #logger.error @customer.name
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def checkAir2
    begin
      #logger.error ">>>>>>>> checkAir2 >>>>>>>>>>>>"
      @from = ""
      @to = ""
      @to2 = ""
      @mid = ""
      @from_airport = ""
      @to_airport = ""
      @to2_airport = ""
      @mid_airport = ""
      @trip_type = ""
      @airways_name = ""
      @airways_code = ""
      @position = ""
      @position2 = ""
      if params[:trip_type]
        @trip_type = params[:trip_type]
        from_id = params[:from_id]
        mid_id = params[:mid_id]
        mid_id2 = params[:mid_id2]
        to_id = params[:to_id]
        airways_id = params[:airways_id]
        @position = params[:position]
        @position2 = params[:position2]
        
        from = Airport.find_by_id(from_id)
        mid = Airport.find_by_id(mid_id)
        to = Airport.find_by_id(to_id)
        airways = Airways.find_by_id(airways_id)
        
        @from = from.airport_code
        @from_airport = from.airport_name
        @to = mid.airport_code
        @to_airport = mid.airport_name
        if @trip_type=="lc" || @trip_type=="wflc"
          @to2 = to.airport_code
          @to2_airport = to.airport_name
          if mid_id2.to_i>0
            mid2 = Airport.find_by_id(mid_id2)
            @mid = mid2.airport_code
            @mid_airport = mid2.airport_name
          end
        end
        @airways_name = airways.airways_name
        @airways_code = airways.airways_code
      end

      @order_no = ""
      @orderid = ""
      phone = params[:phone_number]
      #logger.error phone
      if params[:orderid]
        @orderid = params[:orderid]
        #logger.error @orderid
        order = Order.find_by_id(@orderid)
        if order
          @order_no = order.order_no
          if airways==nil
            air_no = order.tickets[0].air_no
            #logger.error air_no
            airways = Airways.find_by_code(air_no[0,2])
            if airways
              @airways_name = airways.airways_name
              @airways_code = airways.airways_code
            end
          end
          @customer = order.customer
        end
      end
      #logger.error @airways_name
      #logger.error @airways_code
      if @customer==nil && phone
        @customer = Customer.find(:first,:conditions=>["phone=?",phone])
      end
      @customer = Customer.find_by_id(1,:include=>[:customer_type]) if @customer==nil
      #logger.error @customer.name
      #logger.error ">>>>>>>> checkAir2 end >>>>>>>>>>>>"
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #查询转机联运
  def checkZjly
  end
  
  def searchZjly
    begin
      #logger.error ">>>>>>>>>>>> searchZjly >>>>>>>>>>>>>>"
      from_id = params[:from]
      to_id = params[:to]
      date = params[:date]
      type = params[:type]
      airways_code = params[:airways_code]

      type = "" if type==nil
      rootStr = "[]"
      sql = " is_locked<>1 and is_closed<>1 "
      if airways_code && airways_code!=""
        sql << " and airways_code like '%"+airways_code+"%'"
      end
      if type=="wflc"
        sql << " and trip_type='联程往返'"
      elsif type && type=="wf"
        sql << " and trip_type='往返'"
      elsif type=="lc"
        sql << " and trip_type='联程'"
      end
      if from_id && from_id!=""
        sql << " and from_id="+from_id.to_s
        if type=="wf"
          sql << " and to_id="+from_id.to_s
        end
      end
      if to_id && to_id!=""
        if type=="wf"
          sql << " and mid_id="+to_id
        elsif type=="lc"
          sql << " and (to_id="+to_id+" or mid_id="+to_id+")"
        else
          sql << " and (to_id="+to_id+" or mid_id="+to_id+" or mid_id2="+to_id+" or mid_id3="+to_id+" or mid_id4="+to_id+" or end_id="+to_id+")"
        end
      end
      date = Time.new.strftime("%Y-%m-%d") if date==nil
      sql << " and (start_date is null or start_date<='"+date+"') and (end_date is null or end_date>='"+date+"')"
      #logger.error sql
      zs = ZjlyPrice.find(:all,:conditions=>sql,:order=>"price")
      #logger.error zs.length
      if zs && zs.length>0
        for zjly in zs
          airport = Airport.new()
          airport.airport_name = ""
          if zjly.mid_airport2==nil
            zjly.mid_airport2 = airport
          end
          if zjly.mid_airport3==nil
            zjly.mid_airport3 = airport
          end
          if zjly.mid_airport4==nil
            zjly.mid_airport4 = airport
          end
          if zjly.end_airport==nil
            zjly.end_airport = airport
          end
        end
        rootStr = zs.to_json(:include=>[:from_airport,:mid_airport,:mid_airport2,:to_airport,:mid_airport3,:mid_airport4,:end_airport,:airways])
      end
      #logger.error rootStr
      jsonstr="{root:"+rootStr+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #查询航班
  def searchAirLine
    begin
      logger.error "========:controller=#{params[:controller]},:action=#{params[:action]}==========="
      airways_code = params[:airways_code]
      fromto = params[:fromto]
      date = params[:date]
      #zhida_checkbox = params[:zhida_checkbox]
      grid_no = params[:grid_no]
      grid_no = "" if grid_no==nil
      star = params[:star]
      trip_type = params[:trip_type]
      position = params[:position]

      num = 1
      rootStr = "[]"
      result = "successful"
      #转化日期格式
      date_cmd = CommonHelper.getCmdDateFormat(date)
      
      cmd = "AVH/"+fromto+"/"+date_cmd
      if airways_code && airways_code!=""
        cmd << "/"+airways_code
      end
      #直达
      #if zhida_checkbox && zhida_checkbox!=""
      #  cmd += "/"+zhida_checkbox
      #end
      cmd << "/D"
      logger.error "---查询命令cmd="+cmd
      rubySocket = SocketHelper.getSocket(session[:eterm_name],session[:eterm_pswd])
      if rubySocket
        restring = CommonHelper.getAvhResult(rubySocket,cmd,date_cmd)
        #logger.error restring
        if restring.index("失败 ")
          result = restring[restring.index(" ")+1..-1]
        else
          #把不分析的航班取出来.
          flightException = FlightException.find(:first,:conditions=>["fromto = ?",fromto])
          airNoException = nil
          if flightException !=nil
            airNoException = flightException.air_no
            flightException = nil
          end
          #key都是航空公司代码 value都是对应航空公司的hash
          allDiscountHash = Hash.new #所有标准舱位折扣
          allPriceHash = Hash.new    #所有标准舱位价格
          allSpecialHash = Hash.new  #所有特殊舱位
          #Jerry 在此处进行successful判断只是为了打ＬＯＧ吧？，因为在此块中并没有更改result的语句．
          if result.index("successful")
            #logger.error "---All transRes---"
            #logger.error restring
          else
            restring = ""
          end
          rootStr = "["
          #这儿最好用二个参数的形式,记下当前已经搜索到的地方,从当前位置继续搜索.
          while(restring.index("<br>"))
            res = restring[0,restring.index("<br>")].strip
            #logger.error "---------------------Line info----------------------"
            #logger.error res
            #行内容里面必须包含城市对 除了上海有两个机场
            if res.index(" ") && (fromto.index("SHA") || res.index(" "+fromto+" "))
              #air_no_json是有可能带×的航班号 air_no是不带×的航班号
              air_no_json = res[0,res.index(" ")].strip
              #logger.error "air_no_json="+air_no_json
              if air_no_json.index("*")
                if star && star=="yes"
                  air_no = air_no_json[1..-1] #去掉航班前面的*
                else
                  air_no="" #不显示带*的航班
                end
              else
                air_no = air_no_json
              end
              if air_no!="" && (airNoException==nil || airNoException.index(air_no)==nil)
                #如果已处理完成的json中已有该航班 则不再处理改行
                if rootStr.index("'#{air_no}'")==nil
                  airways_code = air_no[0,2]
                  #标准舱位折扣
                  discountHash = allDiscountHash[airways_code]
                  if discountHash==nil
                    discountHash = CommonHelper.getDiscountHash(airways_code)
                    allDiscountHash[airways_code] = discountHash
                  end
                  #标准舱位价格
                  priceHash = allPriceHash[airways_code]
                  if priceHash==nil
                    priceHash = CommonHelper.getPositionPriceHash(air_no,fromto)
                    if priceHash.length==0
                      priceHash = CommonHelper.refreshPositionPrice(rubySocket,air_no,fromto,date_cmd,session[:eterm_name],session[:eterm_pswd])
                    end
                    allPriceHash[airways_code] = priceHash
                  end
                  #特殊舱位
                  specialHash = allSpecialHash[airways_code]
                  if specialHash==nil
                    specialHash = CommonHelper.getSpecialPositionHash(airways_code,fromto,date)
                    allSpecialHash[airways_code] = specialHash
                  end
                  ############## 分析 ##################
                  if res.index("S#")
                    beginNum = res.index("S#")+3
                  elsif res.index("S*")
                    beginNum = res.index("S*")+3
                  else
                    beginNum = res.index(" ")+5
                  end
                  res = res[beginNum..-1].strip
                  #logger.error res
                  
                  if res.index(" "+fromto+" ")
                    fromtoNum = res.index(" "+fromto+" ")
                  elsif fromto.index("SHA")
                    fromtoNum = res.index(" "+fromto.gsub(/SHA/,"PVG")+" ")
                  end
                  #logger.error fromtoNum
                  
                  airStatus = res[0,fromtoNum].strip
                  #logger.error "airStatus="+airStatus
                  airFromTo = res[fromtoNum+1,6].strip
                  #logger.error "airFromTo="+airFromTo
                  flyTime = res[fromtoNum+8,2]+":"+res[fromtoNum+10,2]
                  #logger.error "flyTime="+flyTime
                  arrTime = res[fromtoNum+13,2]+":"+res[fromtoNum+15,2]
                  #logger.error "arrTime="+arrTime
                  plane_type = res[fromtoNum+18,4].strip
                  #logger.error "plane_type="+plane_type
                  #余下部分
                  res7 = res[fromtoNum+22..-1].strip
                  if res7.index("-----")
                    airOther = res7[0,res7.index("-----")].strip
                    statusOther = res7[res7.index("-----")+5..-1].strip
                    if statusOther.index(" ") && statusOther.index(" ")==2
                      statusOther = statusOther[0,statusOther.index("--")].strip if statusOther.index("--")
                      #statusOther = statusOther[0,statusOther.index("-----")] if statusOther.index("-----")
                      airStatus << " " + statusOther
                    else
                      statusOther = ""
                    end
                  else
                    airOther = res7[0..-1].strip
                    statusOther = ""
                  end
                  jt_num = airOther[0,1]
                  if jt_num.to_i>0
                    jt_num = "<a href=javascript:checkJT(\""+air_no+"\",\""+date_cmd+"\") >"+jt_num+"</a>"
                  end
                  #循环舱位信息
                  airStatus = airStatus.strip
                  #logger.error "airStatus="+airStatus
                  
                  airStatus_json = ""
                  while airStatus.length>=2
                    if airStatus.index(" ")
                      k = airStatus[0,2]
                      airStatus = airStatus[3..-1]
                    else
                      k = airStatus
                      airStatus = ""
                    end
                    k1 = k[0,1] #舱位
                    k2 = k[1,1] #座位数
                    #如果position不是空 则是查询特定仓位 只显示该仓位的信息
                    if position=="" || (position!="" && k1==position)
                      special_positionPrice = specialHash[air_no+","+k1] #特价仓位
                      #只显示有座位或者是特价的舱位
                      if (k2=="A" || (k2.to_i>0 && k2.to_i<=9)) || special_positionPrice || k1==position
                        if k1=="F"
                          title = "头等舱"
                        elsif k1=="C"
                          title = "商务舱"
                        elsif k1=="Y"
                          title = "经济舱"
                        elsif discountHash[k1]
                          if discountHash[k1]==0
                            title = "联运"
                          else
                            title = ""+discountHash[k1].to_s+"折"
                          end
                        elsif special_positionPrice && special_positionPrice.position.upcase = k1
                          #logger.error "====特价舱===="+k1
                          title = "特价舱"
                        else
                          title = ""+k1+"舱"
                        end
                        #只显示标准舱位或者特价舱
                        if title.index(k1)==nil
                          price = ""
                          if priceHash[k1] && priceHash[k1].price && priceHash[k1].price>0
                            price = priceHash[k1].price.to_s
                          end
                          if special_positionPrice
                            #特价是红色显示 没有价格
                            title = "<font color=red>"+title+"</font>"
                            price = ""
                          end
                          if price == ""
                            #没有价格的舱位需要pat:a查询价格
                            title = "<a href=javascript:clickOnPat(\""+air_no+"\",\""+airFromTo+"\",\""+k1+"\",\""+flyTime+"\",\""+arrTime+"\",\""+plane_type+"\",\""+grid_no+"\") title=\""+k1+"舱\">"+title+"</a>"
                          else
                            title = "<a href=javascript:clickOn(\""+air_no+"\",\""+airFromTo+"\",\""+k1+"\",\""+price+"\",\""+flyTime+"\",\""+arrTime+"\",\""+plane_type+"\",\""+grid_no+"\") title=\""+k1+"舱\">"+title+"</a>"
                          end
                          #座位数
                          if k2=="A"
                            title << "(>9张)"
                          elsif k2=="S"
                            title << "(候补)"
                          elsif k2=="Q"
                            title << "(申请)"
                          elsif k2.to_i>0 && k2.to_i<=9
                            title << "("+k2+"张)"
                          end
                          #退改签规定
                          if priceHash[k1] && priceHash[k1].policy && priceHash[k1].policy!=""
                            title << "<br><a href=# title="+priceHash[k1].policy+">退改签</a>"
                          elsif price!=""
                            title << "<br><a href=# title=\"无\" >退改签</a>"
                          end
                          title << ""+price+"元" if price != ""
                          #联运舱不显示
                          if title.index("联运")==nil
                            airStatus_json = "<td width=75>"+title+"</td>"+airStatus_json
                          end
                        end
                      end
                    end
                  end
                  #logger.error "airStatus_json="+airStatus_json
                  
                  #airStatus_json = "没有票" if airStatus==""
                  if airStatus_json.index("</td>")
                    if grid_no=="" && trip_type && trip_type==""
                      airStatus_json = "<td width=35><a href=javascript:clickOnApp(\""+air_no+"\",\""+airFromTo+"\",\""+flyTime+"\",\""+arrTime+"\",\""+plane_type+"\") title=\""+k1+"舱\">申请</a></td>"+airStatus_json
                    end
                    airStatus_json = "<table border=0><tr>"+airStatus_json+"</tr></table>"
                    rootStr << "," if rootStr != "["
                    rootStr << "{'no':'"+air_no_json+"','status':'"+airStatus_json+"','fromto':'"+airFromTo+"','flyTime':'"+flyTime+"','arrTime':'"+arrTime+"','plane':'"+plane_type+"','other':'"+jt_num+"'}"
                  end
                end
              end
            end
            restring = restring[restring.index("<br>")+4..-1]
            num += 1
          end
          rootStr << "]"
        end
        if rubySocket
          rubySocket.close
          rubySocket=nil
        end
      else
        result = "连接放大器失败,或用户"+session[:eterm_name].to_s+"已被占用!"
      end
      jsonstr="{totalProperty:"+(num-1).to_s+",root:"+rootStr+",info:'"+result+"'}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      SocketHelper.closeSocket(rubySocket)
    end
  end

  def searchZjlyAirLine
    begin
      logger.error "========:controller=#{params[:controller]},:action=#{params[:action]}==========="
      airways_code = params[:airways_code]
      fromto = params[:fromto]
      date = params[:date]
      
      #转化日期格式
      date_cmd = CommonHelper.getCmdDateFormat(date)

      cmd = "AVH/"+fromto+"/"+date_cmd
      if airways_code && airways_code!=""
        cmd << "/"+airways_code
      end
      logger.error "---查询命令cmd="+cmd
      rubySocket = SocketHelper.getSocket(session[:eterm_name],session[:eterm_pswd])
      if rubySocket
        #restring = CommonHelper.getAvhResult(rubySocket,cmd,date_cmd)
        avResultStr = SocketHelper.getCR(rubySocket,cmd,false)
        #测试专用
        #logger.error "---No.1---"
        #logger.error avResultStr
        if avResultStr.index(date_cmd[0,5])==nil
          #不是以查询日期开头的结构都视为无效结果
          if avResultStr.index("\n")
            avResultStr = avResultStr[0,avResultStr.index("\n")]
          end
          if avResultStr.index("\r")
            avResultStr = avResultStr[0,avResultStr.index("\r")]
          end
          if avResultStr.index("\t")
            avResultStr = avResultStr[0,avResultStr.index("\t")]
          end

          if avResultStr.index("失败")
            restring = avResultStr
          elsif avResultStr.index(date_cmd[2..4])
            restring = "该日没有航班,可以选择"+avResultStr[avResultStr.index(date_cmd[2..4])-2,5]
          elsif avResultStr.index("timeout")
            restring = "连接放大器超时."
          elsif avResultStr.index("socket is null") || avResultStr==""
            restring = "访问放大器失败."
          else
            restring = avResultStr
          end
        else
          #转换字符 去掉第一行的日期信息和最后一行以**开头的其他信息
          res = avResultStr
          res = res.gsub(/\n/,"<br>")
          #res = res[res.index("<br>")+4..-1]             #去掉第一行
          if res.index("**")
            res = res[0,res.index("**")]                 #去掉最后一行
          end
          res = res.gsub(/ /,"&nbsp;")
          restring = res
          #翻页
          pn_sum = 6
          pn_sum = 11 if cmd.index("SHA") && cmd.index("PEK")
          pn_i = 2
          while(pn_i <= pn_sum)
            avResultStr = SocketHelper.getCR(rubySocket,"pn",false)
            if avResultStr.index(date_cmd[0,5])==nil
              #当天航班翻页到头以后就显示下一天的航班了,需要给用户一个提示
              #logger.error "---No."+pn_i.to_s+" OUT!!!!!---"
              break
            else
              #logger.error "---No."+pn_i.to_s+"---"
              #logger.error avResultStr
              
              res = restring
              res = res.gsub(/\n/,"<br>")
              res = res[res.index("<br>")+4..-1]             #去掉第一行
              if res.index("**")
                res = res[0,res.index("**")]                 #去掉最后一行
              end
              res = res.gsub(/ /,"&nbsp;")
              restring = res
              
            end
            pn_i += 1
          end
        end
        result = restring
      else
        result = "连接放大器失败,或用户"+session[:eterm_name].to_s+"已被占用!"
      end
      #logger.error result
      render :text=>result, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      SocketHelper.closeSocket(rubySocket)
    end
  end
  
  def sendSMS
    begin
      require "rjb"
      phone_number = params[:phone_number]
      content = params[:content]
      returnedInfos = Rjb::import("JavaSMS").sendSMS(Constants::SMS_URL,Constants::SMS_USER_NAME,Constants::SMS_PASSWORD,phone_number,content,Constants::SMS_HAVEYD)
      #保存成功的短信息
      if returnedInfos == "SUCCESS" 
        sms = Sms.new
        sms.user_id = session[:user_id]
        sms.content = content
        sms.phone_number = phone_number
        sms.save
      end
      render :text=>returnedInfos, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
end
